package com.cashoutgaming.client.modules.toolbar.button;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Image;
import com.sencha.gxt.core.client.Style.Anchor;
import com.sencha.gxt.core.client.Style.AnchorAlignment;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.menu.Menu;

public class ApplicationToolbarButton extends Component implements HasClickHandlers {

	private Element buttonWrap, imageWrap, textWrap, arrowWrap;
	private ApplicationToolbarButtonStyle style;
	private AnchorAlignment menuAlign = new AnchorAlignment(Anchor.TOP_LEFT, Anchor.BOTTOM_LEFT, true);
	private Menu menu;
	private boolean menuEnabled = false;

	interface ApplicationToolbarButtonStyle extends CssResource {

		String toolbarButtonContainer();

		String toolbarButton();

		String toolbarButton2();

		String toolbarButton2Active();

		String toolbarButtonImage();

		String toolbarButtonText();

		String toolbarButtonText2();

		String toolbarButtonArrow();
		
		String toolbarButtonArrow2();
	}

	interface StyleResources extends ClientBundle {

		@Source("ApplicationToolbarButton.css")
		ApplicationToolbarButtonStyle css();
	}

	public ApplicationToolbarButton(boolean useMenu) {
		final StyleResources resources = GWT.create(StyleResources.class);
		resources.css().ensureInjected();
		style = resources.css();

		setElement(DOM.createTable());
		getElement().setClassName(style.toolbarButtonContainer());

		buttonWrap = DOM.createTD();
		buttonWrap.setClassName(style.toolbarButton2());
		getElement().appendChild(buttonWrap);

		imageWrap = DOM.createDiv();
		imageWrap.setClassName(style.toolbarButtonImage());
		buttonWrap.appendChild(imageWrap);

		textWrap = DOM.createDiv();
		textWrap.setClassName(style.toolbarButtonText2());
		buttonWrap.appendChild(textWrap);

		if (useMenu) {
			menuEnabled = true;
			arrowWrap = DOM.createDiv();
			arrowWrap.setClassName(style.toolbarButtonArrow2());
			buttonWrap.appendChild(arrowWrap);
		}

//		setStyleName(style.toolbarButton());
		sinkEvents(Event.ONCLICK);
		setAllowTextSelection(false);

		addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (menuEnabled) {
					if (menu != null) {
						menu.show(buttonWrap, menuAlign);
					}
				}
			}
		});
	}

	public ApplicationToolbarButton(String text, boolean useMenu) {
		this(useMenu);
		setText(text);
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
		menu.setFocusOnShow(true);
	}

	public void setIcon(ImageResource icon) {
		imageWrap.setInnerHTML("");
		Image img = new Image(icon);
		imageWrap.appendChild(img.getElement());
	}

	public void setText(String text) {
		textWrap.setInnerText(text);
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return addDomHandler(handler, ClickEvent.getType());
	}
}